﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class yxManaPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //数据绑定
            BindData();

        }
        //验证列表视图是否有值
        viewIsNoData();
    }

    //绑定数据源
    private void BindData()
    {
        //为列表视图指定数据源
        gvYX.DataSource = yx_BLL.GetSource();
        //绑定数据到列表视图
        gvYX.DataBind();
    }

    //提交
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        //判断院系名称是否为空
        if (txtYXName.Text.Length == 0)
        {
            ShowMessage("请输入院系名称！");
            return;
        }
        switch (btnRefur.Text)
        {
            case "添加":
                //添加院系
                bool bl = yx_BLL.Add(txtYXName.Text, chkZF.Checked);
                if (bl)
                {
                    ShowMessage("添加成功！");
                    //刷新当前页
                    Response.Redirect("yxPage.aspx");
                }
                else
                    ShowMessage("添加失败！");
                break;
            case "更新":
                //更新院系
                bool b = yx_BLL.Update(updateIndex, txtYXName.Text, chkZF.Checked);
                if (b)
                {
                    ShowMessage("更新成功！");
                    //刷新当前页
                    Response.Redirect("yxPage.aspx");
                }
                else
                    ShowMessage("更新失败！");
                btnRefur.Text = "添加";
                break;
        }
    }

    //翻页
    protected void gvYX_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //指定新的页索引
        gvYX.PageIndex = e.NewPageIndex;
        //重新绑定数据
        BindData();
    }

    //选择行
    public static string updateIndex = "";
    protected void gvYX_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //当前选择项的识别号
        updateIndex = gvYX.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvYX.Rows[e.NewSelectedIndex];        
        txtYXName.Text = row.Cells[1].Text;
        chkZF.Checked = row.Cells[2].Text == "是" ? true : false;
        btnRefur.Text = "更新";
    }

    //取消
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        btnRefur.Text = "添加";
        txtYXName.Text = "";
        chkZF.Checked = false;
    }

    //删除
    protected void gvYX_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //删除数据项
        yx_BLL.Delete(gvYX.DataKeys[e.RowIndex][0].ToString());
        //重新绑定数据
        BindData();
    }

    #region 消息
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        //判断列表视图中是否有记录
        if (gvYX.Rows.Count == 0)
        {
            //列表视图中没有数据，显示提示信息
            RegisterStartupScript("prompt", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未添加相关数据)'</script>");
        }
    }

    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        //注册脚本显示消息
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion


}
